package cg.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import cg.project.pojo.Car;
import cg.project.pojo.Result;
import cg.project.pojo.User;
import cg.project.service.CarService;

import java.util.List;

@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;
    @GetMapping("index")
    public Result index(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "16") int pageSize) {
        if (pageSize == 16) {
            System.out.println("接收到的pageNum参数值：" + pageNum );
            System.out.println("接收到的pageSize参数值：" + pageSize );
            // 这里可以认为是获取所有车辆信息的逻辑，等同于原来的index()方法
            List<Car> carList = carService.getCarList(pageNum, pageSize);
            return new Result("200", "查询", carList);
        } else {
            // 这里是获取指定页码和每页数量的车辆信息的逻辑，等同于原来的index(int, int)方法
            List<Car> carList = carService.getHomePageCars(pageNum, pageSize);
            return new Result("200", "查询", carList);
        }
    }

    @GetMapping("index1")
    public Result index() {
        List<Car> carList = carService.getAllCars();

        return new Result("200", "查询", carList);
    }

    @GetMapping("search")
    public Result search(@RequestParam("keyword") String keyword) {
        List<Car> carList = carService.searchCars(keyword);
        return new Result("200", "搜索", carList);
    }

    // 获取首页车辆列表（带分页）
    @GetMapping("homepage")
    public Result homepage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        List<Car> carList = carService.getHomePageCars(pageNum, pageSize);
        return new Result("200", "查询", carList);
    }

    // 获取车辆列表（带分页）
    @GetMapping("index2")
    public Result index2(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "16") int pageSize) {
        List<Car> carList = carService.getCarList(pageNum, pageSize);
        return new Result("200", "查询", carList);
    }
}
